<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader @enterBtn='addFn' :list='list'></TodoHeader>
    <TodoMain :list='newList' @delFn='delBtnFn'></TodoMain>
    <TodoFooter 
    :list='newList'
    :type='getSel'
    @changBtn ='changFn'
    @clearBtn ='clearFn'
    ></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import "./styles/base.css"
import "./styles/index.css"
    
import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";


export default {
   data() {
    return {
      getSel: "all",
      list: JSON.parse(localStorage.getItem("todolist")) || []
    };
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  methods:{
    delBtnFn(index){
      this.list.splice(index, 1);
    },
    addFn(task){
      this.list.push({
        id:this.list.length ? this.list.at(-1).id +1 :100,
        name:task,
        isDone:false
      });
    },
    changFn(type){
      this.getSel = type
    },
    clearFn(){
      this.list = this.list.filter(item => item.isDone === false)
    }
  },
  computed:{
    newList(){
      if(this.getSel === 'yes'){
        return this.list.filter(item => item.isDone === true)
      }else if(this.getSel === 'no'){
        return this.list.filter(item => item.isDone === false)
      }else{
        return this.list
      }
    }
  },
  watch:{
   list:{
    deep: true,
    handler(){
      localStorage.setItem('todolist',JSON.stringify(this.list))
    }
   }
  }
};
</script>